package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.common.RedisConst;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Member;
import com.itheima.health.pojo.Order;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.MemberService;
import com.itheima.health.service.OrderService;
import com.itheima.health.service.OrderSettingService;
import com.itheima.health.service.SetMealService;
import com.itheima.health.vo.OrderSubmitParam;
import com.sun.org.apache.xpath.internal.operations.Or;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 预约控制期
 */
@RestController
@RequestMapping("/mobile/order")
@Slf4j
public class MobileOrderController {
    @Autowired
    private UserController userController;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private MemberService memberService;

    @Autowired
    private OrderSettingService orderSettingService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private SetMealService setMealService;

    /**
     * 提交预约
     *
     * @return
     */
    @PostMapping("/submit")
    @Transactional
    public Result submit(@RequestBody OrderSubmitParam orderSubmitParam) {

        log.info("预约人信息:{}", orderSubmitParam);
        Date orderDate = orderSubmitParam.getOrderDate();//预约人输入的日期
        String telephone = orderSubmitParam.getTelephone();//预约人手机号
        Integer inCode = Integer.parseInt(orderSubmitParam.getValidateCode());//预约人输入的验证码
        Integer code =(Integer) redisTemplate.opsForValue().get(telephone + "_ORDER");//redis中的验证码
        //预约日期不能为空
        if(orderDate==null){
            return new Result(false,MessageConst.ACTION_FAIL);
        }
        //验证验证码
        if(!inCode.equals(code)){
            return new Result(false,MessageConst.VALIDATECODE_ERROR);
        }
        //通过手机号查询会员
        Member member = memberService.getMemberByPhoneNumber(telephone);
        if(member==null){
            //新会员，登录即注册
            member = new Member();
            member.setName(orderSubmitParam.getName());
            member.setSex(orderSubmitParam.getSex());
            member.setIdCard(orderSubmitParam.getIdCard());
            member.setPhoneNumber(telephone);
            member.setRegTime(new Date());
            memberService.save(member);
        }
        //判断当天是否有预约设置
        OrderSetting orderSetting = orderSettingService.getOrderSettingByOrderDate(orderDate);
        if(orderSetting==null){
            //当天不能进行预约
            return new Result(false,MessageConst.SELECTED_DATE_CANNOT_ORDER);
        }
        //通过会员id,预约时间，套餐id查询当天是否有预约
        Order order = orderService.getOneByCondition(member.getId(), orderDate, orderSubmitParam.getSetMealId());
        if(order!=null){
            //老会员不能重复预约
            return new Result(false,MessageConst.HAS_ORDERED);
        }
        //
        int number = orderSetting.getNumber();
        int reservations = orderSetting.getReservations();
        if(number<=reservations){
            return new Result(false,MessageConst.ORDER_FULL);
        }

        //封装预约对象
        order = new Order();
        order.setMemberId(member.getId());
        order.setOrderDate(orderDate);
        order.setSetmealId(orderSubmitParam.getSetMealId());
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        order.setOrderType(Order.ORDERTYPE_WEIXIN);
        //新增预约
        orderService.save(order);
       //更新预约设置
        orderSetting.setReservations(reservations+1);
        orderSettingService.updateReservationsById(orderSetting);

        return new Result(true, MessageConst.ORDER_SUCCESS, order);
    }

    @GetMapping("/findById")
    public Result findById(Integer id){
        log.info("查询的id是：" + id);
        // 根据对应id查询对象
        Order order = orderService.getOrderById(id);
        Member member = memberService.getMemberById(order.getMemberId());

        Setmeal setmeal = setMealService.getSetmealById(order.getSetmealId());
       //将查询结果封装到map集合中
        Map<String,Object> map = new HashMap<>();
        map.put("member",member.getName());
        map.put("setmeal",setmeal.getName());
        map.put("orderDate", new SimpleDateFormat("yyyy-MM-dd").format(order.getOrderDate()));
        map.put("orderType",order.getOrderType());
        return new Result(true,MessageConst.QUERY_ORDER_SUCCESS,map);
    }
}
